//
//  KoiDataChangeEvent.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 09.04.25.
//

import Foundation

extension Notification.Name {
    static let dataUpdated = Notification.Name("dataUpdated")
    static let tokenInvalid = Notification.Name("tokenInvalid")
    static let openAdd = Notification.Name("openAdd")
    static let homeReload = Notification.Name("homeReload")
    static let toLogin = KoiEvent.toLogin.name
}


class KoiEvent {
    
    var name: Notification.Name
    
    init(_ name: Notification.Name) {
        self.name = name
    }
    
    init(_ name: String) {
        self.name = Notification.Name(name)
    }
    
    static var homeReload: KoiEvent = KoiEvent(Notification.Name("homeReload"))
    // 游客状态下引导登录
    static var toLogin: KoiEvent = KoiEvent(Notification.Name("toLogin"))
    
    
}

extension KoiEvent {
    
    func send(object: Any? = nil) {
        NotificationCenter.default.post(name: name, object: object)
    }
    
    func addObserver(block: @escaping (Notification) -> Void) {
        NotificationCenter.default.addObserver(forName: self.name, object: nil, queue: .main) { notification in
            if notification.name == self.name {
                block(notification)
            }
        }
    }
    
    func removeObserver(_ observer: Any) {
        NotificationCenter.default.removeObserver(observer)
    }
}
